;
(function (w) {
  function PreviewTriage(opts) {
    var containerId = opts.containerId;
    var theframe, options = {}, theWin, orginframe;
    var container = document.querySelector('#' + containerId);

    options.token = opts.token;
    options.baseUrl = opts.baseUrl;
    options.domain = opts.domain;
    if (!container) {
        console.error('请指定节点')
        return
      } else {
        if (container.children && container.children.length) {
          console.error('当前配置节点，含有子节点')
          return
        }
      }

      var origin = {
        container: window.location.origin,
        inner: [options.baseUrl],
        current: null
      }
    if (!window._preview_triage_init_) {
        window._preview_triage_init_ = true
        window.addEventListener('message', receiveMsg, false)
    }
    createFrame();

    function createFrame() {
        var params = new URLSearchParams();
        params.append('v_', Date.now());
        var dataString = Date.now();
        theframe = document.createElement('iframe')
        theframe.src = (options.baseUrl + '/SublimeText3Project/yayi/json/index/xcrm-kq.linkedcare.cn/chat/index.html?v=' + dataString + '/#/'|| '') + 'pages/register-manage?from=saas';
        theframe.style.border = 'none'
        theframe.style.width = '100%'
        theframe.style.height = '100%'

        container.appendChild(theframe)
    }
    function receiveMsg(event) {
        var msgObj = event.data
        // if (origin.inner.indexOf(event.origin) < 0) {
        //   return
        // }
  
        if (msgObj.name === 'previewTriage') {
          theWin = event.source;
          orginframe = event.origin;
          sendConfig();
        }
        if (msgObj.name === 'goToBuyPreCheckTriage') {
          if (typeof opts.goToBuyPreCheckTriage == "function") {
            opts.goToBuyPreCheckTriage();
          }
        }
      }
      function postMessage(data) {
        theWin.postMessage(data, orginframe);
      }
  
      function sendConfig() {
        var config = {
          name: 'preview-triage-config',
          token: options.token
        }
        if (options.domain) {
            config.domain = options.domain;
        }
        postMessage(config)
      }
  }
  w.PreviewTriage = PreviewTriage;
})(window)
